//
//  FetterViewController.m
//  XDLookPic
//
//  Created by wanglong8889@126.com on 14-6-2.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "FetterViewController.h"
#import "XDHeader.h"
#import "XDTools.h"
#import "RegisterViewController.h"
@interface FetterViewController ()

@end

@implementation FetterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self.type isEqualToString:@"QQ"]) {
        self.titleLabel.text = @"绑定QQ";
        
    }else{
        self.titleLabel.text = @"绑定微博";
    }
    [self initFetterViews];
}

-(void)initFetterViews{
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"注册" forState:UIControlStateNormal];
    [rightButton setTintColor:[UIColor whiteColor]];
    [rightButton setFrame:CGRectMake(520/2.0f, 13+aHeight, 60, 25)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [rightButton addTarget:self action:@selector(goToRegister) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationBarView addSubview:rightButton];

    
    
    mScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT -UI_STATUS_BAR_HEIGHT)];
    mScrollView.contentSize=CGSizeMake(UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT -UI_STATUS_BAR_HEIGHT );
    mScrollView.scrollEnabled =YES;
    mScrollView.delegate = self;
    [self.contentView addSubview:mScrollView];
    
    

    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [mScrollView addGestureRecognizer:singleRecognizer];
    
    
    
    
    UIImageView *bgImageView=creatXRImageView(CGRectMake(0, 44, UI_SCREEN_WIDTH , 88), [UIImage imageNamed:@"backGround.png"]);
    bgImageView.backgroundColor=[UIColor whiteColor];
    [mScrollView addSubview:bgImageView];
    
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(8,14, 15, 18)];
    imageView.backgroundColor=[UIColor whiteColor];
    imageView.image =[UIImage imageNamed:@"useName.png"];
    [bgImageView addSubview:imageView];
    self.userTf =creatXRTextField(@"请输入乐宠用户", CGRectMake(VIEW_POINT_MAX_X(imageView) +8,5 ,260,39));
    _userTf.borderStyle= UITextBorderStyleNone;
    _userTf.delegate=self;
    [bgImageView addSubview:_userTf];
    UIImageView *lineImageView=[[UIImageView alloc]initWithFrame:CGRectMake(32, VIEW_POINT_MAX_Y(_userTf)-2, UI_SCREEN_WIDTH -32, 1)];
    //    lineImageView.image =[UIImage imageNamed:@"line.png"];
    lineImageView.backgroundColor =UIColorFromRGB(0xdfdfdf);
    [bgImageView addSubview:lineImageView];
    UIImageView *passImageView=[[UIImageView alloc]initWithFrame:CGRectMake(8, 57, 15, 18)];
    [passImageView setBackgroundColor:[UIColor clearColor]];
    [passImageView setImage:[UIImage imageNamed:@"passdWord"]];
    [bgImageView addSubview:passImageView];
    self.passWordTf =creatXRTextField(@"请输入密码", CGRectMake(VIEW_POINT_MAX_X(passImageView) +8,44, 260, 44));
    _passWordTf.borderStyle= UITextBorderStyleNone;
    _passWordTf.secureTextEntry  =YES;
    [bgImageView addSubview:_passWordTf];
    
    UIButton *bnt=creatXRButton(CGRectMake(20, VIEW_POINT_MAX_Y(bgImageView) + 60, UI_SCREEN_WIDTH -40, 40),@"一键绑定",[UIImage imageNamed:@"确定.png"],[UIImage imageNamed:@"确定2.png"]);
    [bnt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    bnt.tag =UIBUTTON_TAG;
    [bnt setTintColor:[UIColor whiteColor]];
    [bnt addTarget:self action:@selector(goToFetter) forControlEvents:UIControlEventTouchUpInside];
    [mScrollView addSubview:bnt];
    

    
    
}

-(void)goToRegister{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    registerVC.openId = self.openId;
    registerVC.fetVC = self;
    registerVC.type = @"QQ";
    [self.navigationController pushViewController:registerVC animated:YES];
}
//一键绑定
-(void)goToFetter{
    if (!IS_NOT_EMPTY(_userTf.text)||!IS_NOT_EMPTY(_userTf.text)) {
        [XDTools showTips:@"绑定信息不能为空" toView:self.contentView];
    }else{
        [self fetterWithData];
    }
}
-(void)fetterWithData{
    if ([XDTools NetworkReachable])
    {
        
        __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_QQBANGDING]];
        if ([self.type isEqualToString:@"QQ"]) {
            [request setPostValue:@"qqloginadd" forKey:@"action"];
            [request setPostValue:self.openId forKey:@"conopenid"];
        }else{
            [request setPostValue:@"wbloginadd" forKey:@"action"];
            [request setPostValue:self.wbparams.wbId forKey:@"sina_uid"];
            [request setPostValue:self.wbparams.wbToken forKey:@"token"];
            [request setPostValue:self.wbparams.expireTime forKey:@"oauth2_expiretime"];
        }
       
        
        [request setPostValue:_userTf.text forKey:@"username"];
        [request setPostValue:_passWordTf.text forKey:@"password"];
        [request setTimeOutSeconds:12];
        
        [request setRequestCookies:[XDTools productCookies]];
        [request setUseCookiePersistence:NO];
        [request setUserAgentString:@"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0"];
        
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.contentView];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if([[tempDic objectForKey:@"result"]intValue] == 1){
                
                NSDictionary *subDict=[tempDic objectForKey:@"userinfo"];
                NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];
                //存登录信息
                
                [userDef setObject:subDict forKey:kMMyUserInfo];
                [userDef synchronize];
                
                [XDTools showTips:@"绑定成功" toView:self.contentView];
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
                
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [request error];
            [XDTools hideProgress:self.contentView];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
        }];
        [XDTools showProgress:self.contentView];
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void)handleSingleTapFrom{
    [_userTf resignFirstResponder];
    [_passWordTf resignFirstResponder];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
